#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm> // For std::max and std::min
#include <cmath> // For M_PI if needed
#include "../spline_functions.h"
// #include "matplotlibcpp.h"

// namespace plt = matplotlibcpp;

int main() {
    std::cout << "-------Problem 5: Testing ArbitraryOrder B-spline by the function 'y = 1'-------" << std::endl;
    // 定义节点和系数 coefficients.size() == degree + knots.size() - 1
    std::vector<double> knots = {1, 2, 3, 4, 5, 6, 7}; // 节点序列
    std::vector<double> coefficients = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; // 系数序列，假设为三次B样条

    // 定义B样条的次数
    int degree = 4;

    // 创建B样条对象
    ArbitraryOrderBSpline spline(degree, knots, coefficients);

    // 定义一系列点来评估B样条
    std::vector<double> x_values = {1.3, 1.5, 2.4, 3.5, 4.1, 5.8, 6.9};
    std::vector<double> y_values;

    // 评估B样条在x_values上的值
    for (double x : x_values) {
        try {
            double value = spline.evaluate(x);
            y_values.push_back(value);
            std::cout << "B spline value at x = " << x << " is " << value << std::endl;
        } catch (const std::out_of_range& e) {
            std::cout << "error: " << e.what() << std::endl;
        }
    }

    // // 定义一系列点来评估B样条
    // std::vector<double> x_values;
    // for (double x = 1; x <= 7; x += 0.01) {
    //     x_values.push_back(x);
    // }

    // std::vector<double> y_values;

    // // 评估B样条在x_values上的值
    // for (double x : x_values) {
    //     try {
    //         double value = spline.evaluate(x);
    //         y_values.push_back(value);
    //     } catch (const std::out_of_range& e) {
    //         std::cout << "错误: " << e.what() << std::endl;
    //         y_values.push_back(0); // 在错误点设置y值为0
    //     }
    // }

    // // 绘制B样条函数图像
    // plt::plot(x_values, y_values);
    // plt::title("B spline value");
    // plt::xlabel("x");
    // plt::ylabel("y");
    // plt::show();

    return 0;
}